//
//  CZStatusOriginalView.swift
//  GZWeibo009
//
//  Created by gzxzmac on 16/3/25.
//  Copyright © 2016年 gzxzmac. All rights reserved.
//

import UIKit

import SnapKit

/// 原创微博的view
class CZStatusOriginalView: UIView {
    
    // MARK: - 属性
    /// 底部约束的属性
    var originalViewBottomCons: Constraint?
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            // 当别人来设置微博模型的时候,我们根据微博模型来设置原创微博的相关内容
            /// 1.设置头像
            if let imageUrl = status?.user?.profile_image_url {
                let url = NSURL(string: imageUrl)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            /// 设置名称
            nameLabel.text = status?.user?.screen_name
            
            /// 设置时间
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDateDescripiton()
            
            /// 来源
            sourceLabel.text = status?.source
            
            // 微博模型提供的数据不是很合理,需要view在显示的时候来计算需要的图片,如果其他地方也需要设置会员等级,又要这段重复的代码,在view即将显示的时来计算是需要消耗cpu,不要在cell的view里面来计算
            /// 会员等级, 直接找模型要图片
            memberView.image = status?.user?.mbrankImage
            //            if let mbrank = status?.user?.mbrank {
            //                if mbrank > 0 && mbrank <= 6 {
            //                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
            //                } else {    // 防止cell复用,没有会员的就不要等级
            //                    memberView.image = nil
            //                }
            //            }
            
            /// 认证图标,直接找模型要图片
            verifiedView.image = status?.user?.verifiedTypeImage
            //            if let verified_type = status?.user?.verified_type {
            //                var image: UIImage?
            //
            //                switch verified_type {
            //                case 0:
            //                    image = UIImage(named: "avatar_vip")
            //                case 2,3,5:
            //                    image = UIImage(named: "avatar_enterprise_vip")
            //                case 220:
            //                    image = UIImage(named: "avatar_grassroot")
            //                default:
            //                    image = nil
            //                }
            //
            //                verifiedView.image = image
            //            }
            
            /// 设置微博内容
            contentLabel.text = status?.text
            
            // 判断微博是否要显示图片,要显示图片才显示CZStatusPictureView,如果不要显示就隐藏掉
            // 判断是否要显示图片,先判断pic_urls这个可选是否有值,如果有值还要判断数量是否>0
            
            // 在添加约束之前删除这个约束
            originalViewBottomCons?.uninstall() // 删除这个约束
            //            if let pictureURLs = status?.pic_urls where pictureURLs.count > 0 {
            
            if let pictureURLs = status?.storePictureURLs {
                // 有图片需要显示
                picutreView.hidden = false
                
                // 给picutreView赋值pictureURLs数组
                picutreView.pictureURLs = pictureURLs
                
                // 需要修改约束,不然还会一直参考picutreView
                // 有图片需要显示CZStatusOriginalView的底部就参照picutreView的底部
                // 使用snp_updateConstraints不能更换参照
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomCons = make.bottom.equalTo(picutreView).offset(CZStatusCellMargin).constraint
                })
            } else {
                // 没有图片需要显示
                picutreView.hidden = true
                // 没有图片需要显示CZStatusOriginalView的底部就参照conetntLabel的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomCons = make.bottom.equalTo(contentLabel).offset(CZStatusCellMargin).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(picutreView)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(CZStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(CZStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        /// 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(CZStatusCellMargin)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        /// 配图视图
        picutreView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        /// CZStatusOriginalView的底部参照picutreView的底部
        self.snp_makeConstraints { (make) -> Void in
            // constraint就是这行代码所创建的约束
            self.originalViewBottomCons = make.bottom.equalTo(picutreView).offset(CZStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellBottomDefaultFontSize)
        
        return label
    }()
    
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView()
    
    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZStatusCellContentSize)
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 配图视图,不管cell是否要显示图片,先加载进来,如果模型里面不需要显示图片,就把这个配图视图隐藏掉,如果要显示图片就把这个配图视图显示出来
    private lazy var picutreView: CZStatusPictureView = CZStatusPictureView()
}
